/*
 * CS_GUILD_WRITE_MAIL.cpp
 *
 *  Created on: 2014. 8. 5.
 *      Author: mega
 */

#include "../../User.h"
#include "../../Command.h"
#include "../../Proxy.h"

#include "../../NUtil/Utils.h"
#include "../../NLogic/Function.h"

#include "../protocol/Guild.h"

namespace ProjectZ { namespace NCommand {

static void SendFail(User * pUser, int ack)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ack);
	pAck->MakePacket(CMD_SC_GUILD_WRITE_MAIL);
	pUser->Send(pAck);
}

void Command::CS_GUILD_WRITE_MAIL(User * pUser, ::Network::Packet * pPacket)
{
/*	//길드 편지 보내기
	CS_GUILD_WRITE_MAIL			0x1210
	{
		u32				u32user_seq;				// 받는 사람 Seq
		u64				u64item_seq;				// 아이템 seq
		u32				u32gold;					// 보낼 골드
		u8				u8bag_Type;					// 가방 타입
		str				strmail_message;			// 편지 내용
	}

	//길드 편지 보내기
	SC_GUILD_WRITE_MAIL			0x1211
	{
		s16				ack;							// 1: 보내기 성공, -1: 실패, -2: 없는 아이디
	}*/

	PREPARE_PACKET;

	uint32_t userSeq	= 0;
	uint64_t itemSeq	= 0;
	uint32_t gold		= 0;
	uint8_t bagType		= 0;
	Toolkit::Str<180> message;

	*pPacket >> userSeq >> itemSeq >> gold >> bagType >> message;

	if (false == NLogic::NFunction::IsRegisterUser(userSeq))
	{
		SendFail(pUser, ACK_NOT_EXIST_USER);
		return;
	}

	if (0 >= pUser->GetUserInfo().guild_seq)
	{
		SendFail(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	// 수수료10%
	int tax = gold * (10 / 100.0);
	if (0 > tax)
		tax = 0;

	if ((gold + tax) > pUser->GetCharacterInfo().gold)
	{
		SendFail(pUser, ACK_NOT_ENOUGH_GOLD);
		return;
	}

	SendGuildMailCheckSyn syn;
	syn.user_seq		= pUser->GetUserSeq();
	syn.guild_seq		= pUser->GetUserInfo().guild_seq;
	syn.recv_user_seq	= userSeq;
	syn.item_seq		= itemSeq;
	syn.bag_type		= bagType;
	syn.gold			= gold;
	syn.tax				= tax;
	syn.msg				= message.c_str();

	if (false == NProxy::SendGuildServer(pUser, syn))
	{
		SendFail(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

}

} /*NCommand*/ } /*ProjectZ*/
